import pandas as pd
import json
import sqlalchemy as sa
from pyecharts import options as opts
from pyecharts.charts import Map, Bar, Line, Pie, WordCloud
from pyecharts.globals import ChartType
from pyecharts.options import ItemStyleOpts
from pyecharts.globals import ThemeType


def getengine():
    engine = sa.engine.create_engine(
        "mysql+pymysql://root:COVID19@49.235.74.25:3000/COVID",
        encoding="utf-8",
    )
    return engine

def gett_today():
    df = pd.read_sql('db_china_today', getengine())
    return df.to_dict()


colors = {"today_confirm":["white", "yellow", "orangered", "#C1232B"],"total_confirm":["white", "yellow", "orangered", "#C1232B"],"total_nowConfirm":["white", "yellow", "orangered", "#C1232B"],"total_suspect":["white", "blue", "#EC690C"],"total_dead" : ["white", "#4C4E80","#701380"],"total_heal" : ["white", "yellow", "green"],"total_deadRate" : ["white", "#4C4E80","#701380"],"total_healRate" : ["white", "yellow", "green"],}

ma = {"today_confirm":50,"total_confirm":1200,"total_nowConfirm":100,"total_suspect":1200,"total_dead":1200,"total_heal":1200,"total_deadRate": 10,"total_healRate" : 100,}

gt = {
    'update_time':'日期',
    'total_confirm':'累计确诊',
    'today_confirm':'当天确诊',
    'total_heal':'累计治愈',
    'today_heal':'当天治愈',
    'total_nowconfirm':'现存确诊',
    'today_nowconfirm':'今日现存确诊',
    'total_dead':'累计死亡',
    'today_dead':'当天死亡',
    'total_suspect':'累计疑似感染',
    'today_suspect':'当天新增疑似感染',
}

#base_data,line_data,tp = getdata()

#china_map(base_data,tp)
#china_bar(base_data,tp)
#china_pie(base_data,tp)
#china_wcd(base_data,tp)

#china_accumulate(line_data, tp)
#china_today(line_data, tp)



tp = {'contry': 'china', 'ptname': '中国', 'usebase': '累计确诊', 'title': 'total_confirm'}
df = pd.read_sql('db_province', getengine())
df = df.sort_values(tp['title'])
base_data = []
for index, row in df.iterrows():
    base_data.append([row["name"], row[tp['title']]])
df = pd.read_sql('db_china_history',getengine())
line_data = df.to_dict(orient="dict")
base_data, line_data, tp


def china_ppie():
    c = (
        Pie()
            .add("", base_data,
                 radius=["40%", "75%"],
                 label_opts=opts.LabelOpts(is_show=False), )
            .set_global_opts(
            title_opts=opts.TitleOpts(title="中国" + tp['usebase'] + "饼状图"),
            legend_opts=opts.LegendOpts(is_show=True, type_='scroll', pos_bottom='0%',
                                        orient='horizontal', ),
        )
            .set_series_opts()
    ).dump_options_with_quotes()
    return c

